package com.broada.apm.dao.impl;

import java.util.HashMap;
import java.util.List;

import com.broada.apm.dao.IndicatorDao;
import com.broada.apm.model.DictMetrics;
import com.broada.apm.model.L7ModuelPanel;
import com.broada.apm.model.L7moduleMetric;

public class MysqlIndicatorDao extends MysqlAbstractDao implements IndicatorDao {
	
	private static final String NAMESPACE = "com.broada.apm.dao.IndicatorDao.";

	@Override
	public List<DictMetrics> listItems() {
		return getSqlSession().selectList(NAMESPACE + "getIndicatorItems");
	}

	@Override
	public List<L7ModuelPanel> getPanleIndicatorItemByProtocol(L7ModuelPanel panel) {
		return getSqlSession().selectList(NAMESPACE + "getPanleIndicatorItemByProtocol", panel);
	}

	@Override
	public List<DictMetrics> getDictMetricsByProtocol(int l7protocol) {
		return getSqlSession().selectList(NAMESPACE + "getDictMetricsByProtocol", l7protocol);
	}

	@Override
	public DictMetrics getItemById(int itemId) {
		return getSqlSession().selectOne(NAMESPACE + "getIndicatorItemByItemId", itemId);
	}

	@Override
	public DictMetrics getDictMetricsByCode(String code) {
		return getSqlSession().selectOne(NAMESPACE + "getDictMetricsByCode", code);
	}

	@Override
	public void addDictMetrics(DictMetrics dictMetrics) {
		getSqlSession().insert(NAMESPACE + "addDictMetrics", dictMetrics);
	}

	@Override
	public void addL7ModuleMetric(L7moduleMetric l7Metric) {
		getSqlSession().insert(NAMESPACE + "addL7ModuleMetric", l7Metric);
	}

	@Override
	public void addL7ModulePanel(L7ModuelPanel panel) {
		getSqlSession().insert(NAMESPACE + "addL7ModulePanel", panel);
	}

	@Override
	public List<L7moduleMetric> getL7ModuleMetric(L7moduleMetric l7Metric) {
		return getSqlSession().selectList(NAMESPACE + "getL7ModuleMetric", l7Metric);
	}

	@Override
	public List<L7ModuelPanel> getL7ModulePanel(L7ModuelPanel panel) {
		return getSqlSession().selectList(NAMESPACE + "getL7ModulePanel", panel);
	}

	@Override
	public void deleteMetricsByModuleId(String moduleId) {
		getSqlSession().delete(NAMESPACE + "deleteMetricsByModuleId", moduleId);
	}

	@Override
	public void updateDictMetrics(DictMetrics dictMetrics) {
		getSqlSession().update(NAMESPACE + "updateDictMetrics", dictMetrics);
	}

	@Override
	public void updateL7ModuleMetric(L7moduleMetric l7Metric) {
		if(l7Metric.getMetricCaption() != null)
			getSqlSession().update(NAMESPACE + "updateL7ModuleMetric", l7Metric);
	}

	@Override
	public void deleteL7PanelByModuleId(String moduleId) {
		getSqlSession().delete(NAMESPACE + "deleteL7PanelByModuleId", moduleId);
	}

	@Override
	public void updateL7ModulePanel(L7ModuelPanel panel) {
		getSqlSession().update(NAMESPACE + "updateL7ModulePanel", panel);
	}

	@Override
	public List<L7moduleMetric> listItemsByCondition(L7moduleMetric conn) {
		return getSqlSession().selectList(NAMESPACE + "listItemsByCondition", conn);
	}

	@Override
	public void updateResShowIndicator(String targetId, String panelId, List<Integer> metricIds) {
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("targetId", targetId);
		map.put("panelId", panelId);
		map.put("metricIds", metricIds);
		
		getSqlSession().delete(NAMESPACE + "deleteResShowIndicatorMap", map);
		getSqlSession().insert(NAMESPACE + "addResShowIndicatorMap", map);
	}

	@Override
	public List<Integer> getResShowIndicator(String targetId, String panelId) {
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("targetId", targetId);
		map.put("panelId", panelId);
		return getSqlSession().selectList(NAMESPACE + "getResShowIndicator", map);
	}
	
}
